package com.hex.liyue.usermodule.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.usermodule.adapter.AdapterWallet;
import com.hex.liyue.usermodule.bean.MyWalletResultbean;
import com.hex.liyue.usermodule.presenter.IWalletView;
import com.hex.liyue.usermodule.presenter.WalletPresenter;

import java.text.DecimalFormat;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/15.
 * 我的钱包界面
 */
public class WalletActivity extends BaseActivity implements IWalletView, View.OnClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.rv_waller_list)
    YRecycleview rvWallerList;

    private WalletPresenter walletPresenter;
    private AdapterWallet adapterWallet;
    private int page = 1, page_num = 5;
    private TextView tv_balance, tv_bank_info, tv_gift_total_price, tv_cash_total_price, tv_more_records;
    private long balance;

    @Override
    protected void bindPresenter() {
        walletPresenter = new WalletPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        View headView = View.inflate(this, R.layout.head_wallet, null);
        tv_balance = (TextView) headView.findViewById(R.id.tv_balance);
        tv_bank_info = (TextView) headView.findViewById(R.id.tv_bank_info);
        tv_bank_info.setOnClickListener(this);
        tv_gift_total_price = (TextView) headView.findViewById(R.id.tv_gift_total_price);
        tv_cash_total_price = (TextView) headView.findViewById(R.id.tv_cash_total_price);
        tv_more_records = (TextView) headView.findViewById(R.id.tv_more_records);
        tv_more_records.setOnClickListener(this);

        adapterWallet = new AdapterWallet();
        rvWallerList.addHeadView(headView);
        rvWallerList.setAdapter(adapterWallet);
        rvWallerList.setLayoutManager(new LinearLayoutManager(this));
        rvWallerList.setReFreshEnabled(false);
        rvWallerList.setLoadMoreEnabled(false);

        walletPresenter.getWallet(page, page_num);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarRight.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("我的钱包");
        tvToobarRight.setText("我的银行卡");
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_toobar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_toobar_right:
                //银行卡信息
                startUserBankActivity(false, 0);
                break;
            case R.id.tv_more_records:
                //更多交易记录
                startMoreRecordsActivity();
                break;
            case R.id.tv_bank_info:
                //提现
                if (balance <= 2000) {
                    startwxchattithdrawalsActivity();
                    return;
                }
                if (balance > 0) {
                    startUserBankActivity(true, balance);
                } else {
                    //余额不够提现
                    showT("您的余额还不够提现哦！");
                }
                break;
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void setAdapter(MyWalletResultbean myWalletResultbean) {
        tv_balance.setText("￥" + qianweifenge(myWalletResultbean.data.balance));
        tv_bank_info.setEnabled(false);
        if (myWalletResultbean.data.audit.equals("-1")) {
            tv_bank_info.setText("认证失败");
        } else if (myWalletResultbean.data.audit.equals("-2")) {
            tv_bank_info.setText("没有银行卡");
        } else if (myWalletResultbean.data.audit.equals("1")) {
            tv_bank_info.setText("认证中");
        } else if (myWalletResultbean.data.audit.equals("2")) {
            tv_bank_info.setText("申请提现");
            tv_bank_info.setTextColor(Color.WHITE);
            tv_bank_info.setEnabled(true);
        }

        if (myWalletResultbean.data.balance <= 2000) {
            tv_bank_info.setEnabled(true);
            tv_bank_info.setText("提现");
            tv_bank_info.setTextColor(getResources().getColor(R.color.btn_white_normal));
            tv_bank_info.setBackgroundResource(R.drawable.bg_black_write_stroken);
            tvToobarRight.setVisibility(View.GONE);
        }

        balance = myWalletResultbean.data.balance;
        tv_gift_total_price.setText("￥" + myWalletResultbean.data.gift_total_price);
        tv_cash_total_price.setText("￥" + myWalletResultbean.data.cash_total_price);
        adapterWallet.setRecords(myWalletResultbean.data.records);
        adapterWallet.notifyDataSetChanged();
    }

    public static String qianweifenge(long num) {
        DecimalFormat df = new DecimalFormat("#,###");
        String ss = df.format(num);
        return ss;
    }

    @Override
    public void startUserBankActivity(boolean flag, long balance) {
        Intent intent = new Intent(this, UserBankActivity.class);
        intent.putExtra("flag", flag);
        intent.putExtra("balance", balance);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            walletPresenter.getWallet(page, page_num);
        }
    }

    @Override
    public void startMoreRecordsActivity() {
        Intent intent = new Intent(this, MoreRecordsActivity.class);
        startActivity(intent);
    }

    @Override
    public void startwxchattithdrawalsActivity() {
        Intent intent = new Intent(this, WeChatWithdrawalsActivity.class);
        startActivity(intent);
    }
}
